package com.entertainment.ticketing.config;

import com.entertainment.ticketing.entity.EventInfo;
import com.entertainment.ticketing.entity.TicketInfo;
import com.entertainment.ticketing.entity.UserInfo;
import com.entertainment.ticketing.repository.EventInfoRepository;
import com.entertainment.ticketing.repository.TicketInfoRepository;
import com.entertainment.ticketing.repository.UserInfoRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Configuration
public class DataSeeder {

    @Bean
    CommandLineRunner seedData(EventInfoRepository eventRepo,
                               TicketInfoRepository ticketRepo,
                               UserInfoRepository userRepo,
                               PasswordEncoder passwordEncoder) {
        return args -> {
            // Seed users
            if (userRepo.findByUsername("admin").isEmpty()) {
                UserInfo admin = new UserInfo();
                admin.setUsername("admin");
                admin.setEmail("admin@example.com");
                admin.setPassword(passwordEncoder.encode("admin123"));
                admin.setPhone("13900000000");
                admin.setRealName("管理员");
                admin.setStatus((byte)1);
                admin.setRole("ADMIN");
                userRepo.save(admin);
            }
            if (userRepo.findByUsername("demo").isEmpty()) {
                UserInfo demo = new UserInfo();
                demo.setUsername("demo");
                demo.setEmail("demo@example.com");
                demo.setPassword(passwordEncoder.encode("password123"));
                demo.setPhone("13900000001");
                demo.setRealName("演示用户");
                demo.setStatus((byte)1);
                demo.setRole("USER");
                userRepo.save(demo);
            }

            // Seed events and tickets (only if no events exist)
            if (eventRepo.count() == 0) {
                EventInfo e1 = new EventInfo();
                e1.setTitle("《歌剧魅影》音乐剧");
                e1.setCategory("音乐剧");
                e1.setVenue("上海大剧院");
                e1.setAddress("上海市黄浦区某路1号");
                e1.setDescription("经典音乐剧《歌剧魅影》，改编自加斯东·勒鲁同名小说，现场乐队与华丽布景带来震撼视听体验。");
                e1.setStartTime(LocalDateTime.now().plusDays(7));
                e1.setEndTime(LocalDateTime.now().plusDays(7).plusHours(3));
                e1.setPosterUrl("/images/phantom-opera.svg");
                e1.setStatus((byte)1);
                e1.setTotalSeats(500);
                e1.setAvailableSeats(500);

                EventInfo e2 = new EventInfo();
                e2.setTitle("周杰伦演唱会");
                e2.setCategory("演唱会");
                e2.setVenue("北京工人体育馆");
                e2.setAddress("北京市朝阳区某路2号");
                e2.setDescription("华语流行天王周杰伦巡回演唱会，北京站热力开唱，经典曲目联袂现场互动。");
                e2.setStartTime(LocalDateTime.now().plusDays(14));
                e2.setEndTime(LocalDateTime.now().plusDays(14).plusHours(2));
                e2.setPosterUrl("/images/jay-concert.svg");
                e2.setStatus((byte)1);
                e2.setTotalSeats(800);
                e2.setAvailableSeats(800);

                EventInfo e3 = new EventInfo();
                e3.setTitle("开心麻花《乌龙山伯爵》");
                e3.setCategory("话剧");
                e3.setVenue("深圳保利剧院");
                e3.setAddress("深圳市南山区某路3号");
                e3.setDescription("开心麻花口碑话剧，笑点密集、剧情反转不断，适合亲友结伴观演。");
                e3.setStartTime(LocalDateTime.now().plusDays(21));
                e3.setEndTime(LocalDateTime.now().plusDays(21).plusHours(2));
                e3.setPosterUrl("/images/wulong-count.svg");
                e3.setStatus((byte)1);
                e3.setTotalSeats(600);
                e3.setAvailableSeats(600);

                EventInfo e4 = new EventInfo();
                e4.setTitle("天鹅湖芭蕾舞剧");
                e4.setCategory("舞蹈");
                e4.setVenue("广州大剧院");
                e4.setAddress("广州市天河区某路4号");
                e4.setDescription("芭蕾经典《天鹅湖》，国际舞团领衔出演，优雅与力量兼具的艺术盛宴。");
                e4.setStartTime(LocalDateTime.now().plusDays(30));
                e4.setEndTime(LocalDateTime.now().plusDays(30).plusHours(3));
                e4.setPosterUrl("/images/swan-lake.svg");
                e4.setStatus((byte)1);
                e4.setTotalSeats(700);
                e4.setAvailableSeats(700);

                // 更多中文演出
                EventInfo e5 = new EventInfo();
                e5.setTitle("京剧名段荟萃专场");
                e5.setCategory("戏曲");
                e5.setVenue("国家大剧院");
                e5.setAddress("北京市西城区某路5号");
                e5.setStartTime(LocalDateTime.now().plusDays(5));
                e5.setEndTime(LocalDateTime.now().plusDays(5).plusHours(2));
                e5.setPosterUrl("/images/beijing-opera.svg");
                e5.setStatus((byte)1);
                e5.setTotalSeats(400);
                e5.setAvailableSeats(400);
                e5.setDescription("国粹京剧精选名段，行当齐全，名家云集，带来原汁原味的传统艺术。");

                EventInfo e6 = new EventInfo();
                e6.setTitle("现代舞《流影》");
                e6.setCategory("舞蹈");
                e6.setVenue("上海文化广场");
                e6.setAddress("上海市徐汇区某路6号");
                e6.setStartTime(LocalDateTime.now().plusDays(9));
                e6.setEndTime(LocalDateTime.now().plusDays(9).plusHours(2));
                e6.setPosterUrl("/images/modern-dance.svg");
                e6.setStatus((byte)1);
                e6.setTotalSeats(500);
                e6.setAvailableSeats(500);
                e6.setDescription("先锋编舞与多媒体装置融合，探索身体与空间的关系，极具当代美学风格。");

                EventInfo e7 = new EventInfo();
                e7.setTitle("交响音乐会·致敬经典");
                e7.setCategory("音乐会");
                e7.setVenue("广州星海音乐厅");
                e7.setAddress("广州市越秀区某路7号");
                e7.setStartTime(LocalDateTime.now().plusDays(12));
                e7.setEndTime(LocalDateTime.now().plusDays(12).plusHours(2));
                e7.setPosterUrl("/images/symphony.svg");
                e7.setStatus((byte)1);
                e7.setTotalSeats(600);
                e7.setAvailableSeats(600);
                e7.setDescription("世界名曲精选，国内一线乐团演绎，恢弘音响与现场氛围令人沉醉。");

                EventInfo e8 = new EventInfo();
                e8.setTitle("魔术秀《幻境》");
                e8.setCategory("魔术");
                e8.setVenue("深圳保利剧院");
                e8.setAddress("深圳市南山区某路8号");
                e8.setStartTime(LocalDateTime.now().plusDays(16));
                e8.setEndTime(LocalDateTime.now().plusDays(16).plusHours(2));
                e8.setPosterUrl("/images/magic-show.svg");
                e8.setStatus((byte)1);
                e8.setTotalSeats(450);
                e8.setAvailableSeats(450);
                e8.setDescription("国际魔术大师联袂献艺，大型舞台机关与近景互动穿插，惊喜不断。");

                EventInfo e9 = new EventInfo();
                e9.setTitle("脱口秀之夜");
                e9.setCategory("脱口秀");
                e9.setVenue("北京开心剧场");
                e9.setAddress("北京市朝阳区某路9号");
                e9.setStartTime(LocalDateTime.now().plusDays(3));
                e9.setEndTime(LocalDateTime.now().plusDays(3).plusHours(2));
                e9.setPosterUrl("/images/standup-comedy.svg");
                e9.setStatus((byte)1);
                e9.setTotalSeats(300);
                e9.setAvailableSeats(300);
                e9.setDescription("新锐与资深演员同台放送段子，轻松幽默，包袱不断，适合朋友聚会放松。");

                EventInfo e10 = new EventInfo();
                e10.setTitle("儿童剧《森林奇遇记》");
                e10.setCategory("儿童剧");
                e10.setVenue("成都儿童剧场");
                e10.setAddress("成都市锦江区某路10号");
                e10.setStartTime(LocalDateTime.now().plusDays(4));
                e10.setEndTime(LocalDateTime.now().plusDays(4).plusHours(2));
                e10.setPosterUrl("/images/children-play.svg");
                e10.setStatus((byte)1);
                e10.setTotalSeats(500);
                e10.setAvailableSeats(500);
                e10.setDescription("寓教于乐的儿童舞台剧，互动环节丰富，家庭亲子观演首选。");

                List<EventInfo> savedEvents = eventRepo.saveAll(List.of(e1, e2, e3, e4, e5, e6, e7, e8, e9, e10));

                // Create some tickets for each event
                for (EventInfo ev : savedEvents) {
                    for (int i = 1; i <= 20; i++) {
                        TicketInfo t = new TicketInfo();
                        t.setEvent(ev);
                        t.setTicketType(i <= 2 ? "VIP" : "STANDARD");
                        t.setPrice(i <= 2 ? new BigDecimal("680") : new BigDecimal("280"));
                        t.setSeatArea(i <= 10 ? "A" : "B");
                        t.setSeatRow(i <= 10 ? "1" : "2");
                        t.setSeatNumber(String.valueOf(i));
                        t.setStatus((byte)1); // 可售
                        ticketRepo.save(t);
                    }
                }
            }
        };
    }
}